package com.ios.service.impl;

import com.ios.dao.AttentionMapper;
import com.ios.dao.DisablerUserMapper;
import com.ios.dto.AttentionExecution;
import com.ios.entity.Attention;
import com.ios.enums.AttentionStateEnum;
import com.ios.service.IAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AttentionServiceImpl implements IAttentionService {

    @Autowired
    private AttentionMapper attentionMapper;
    @Autowired
    private DisablerUserMapper disablerUserMapper;

    @Override
    public AttentionExecution AddAttention(Integer disablerId1, Integer disablerId2) {
        if (!CheckDisablerId(disablerId1)){
            /*检查用户是否存在*/
            return new AttentionExecution(AttentionStateEnum.NULL);
        }else if (!CheckDisablerId(disablerId2)){
            /*检查用户是否存在*/
            return new AttentionExecution(AttentionStateEnum.NULL);
        }else if(CheckAttention(disablerId1,disablerId2)){
            /*检查是否已关注*/
            return new AttentionExecution(AttentionStateEnum.REPEAT);
        }else{
            /*添加关注信息*/
            try {
                Attention attention = new Attention();
                attention.setCreateTime(new Date());
                attention.setUpdateTime(new Date());
                attention.setDisablerId1(disablerId1);
                attention.setDisablerId2(disablerId2);
                attentionMapper.insert(attention);
                return new AttentionExecution(AttentionStateEnum.A_SUCCESSS,attention);
            }catch (Exception e){
                return new AttentionExecution(AttentionStateEnum.A_FAIL);
            }
        }
    }

    @Override
    public AttentionExecution DeleteAttention(Integer disablerId1, Integer disablerId2) {
        if (!CheckDisablerId(disablerId1)){
            /*检查用户是否存在*/
            return new AttentionExecution(AttentionStateEnum.NULL);
        }else if (!CheckDisablerId(disablerId2)){
            /*检查用户是否存在*/
            return new AttentionExecution(AttentionStateEnum.NULL);
        }else if (!CheckAttention(disablerId1,disablerId2)){
            /*检查关注记录是否存在*/
            return new AttentionExecution(AttentionStateEnum.ERROR_ATTENTION);
        }else {
            /*删除关注信息*/
            try{
                attentionMapper.deleteByDisablerId(disablerId1,disablerId2);
                return new AttentionExecution(AttentionStateEnum.UA_SUCCESS);
            }catch (Exception e){
                return new AttentionExecution(AttentionStateEnum.UA_FAIL);
            }
        }
    }

    /*判断用户是否存在*/
    private boolean CheckDisablerId(Integer disablerId){
        List<Integer > AllId = disablerUserMapper.getAllDisablerId();
        for (Integer id : AllId){
            if (id.equals(disablerId)){
                return true;
            }
        }
        return false;
    }

    /*判断是否已关注*/
    private boolean CheckAttention(Integer disablerId1,Integer disablerId2){
        List<Attention> attentionList = attentionMapper.ShowAllAttention();
        for (Attention attention:attentionList){
            if (attention.getDisablerId1()==disablerId1
                    &&attention.getDisablerId2()==disablerId2){
                return true;
            }
        }
        return false;
    }
}
